package com.it.controller;

import com.it.context.BaseContext;
import com.it.pojo.dto.KnowledgeAddDTO;
import com.it.pojo.dto.KnowledgeListDTO;
import com.it.pojo.entity.PageResult;
import com.it.pojo.entity.Result;
import com.it.pojo.vo.KnowledgeFileVO;
import com.it.service.KnowledgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user/knowledge")
@Slf4j
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    /**
     * 获取知识库列表
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult> list(KnowledgeListDTO knowledgeListDTO) {
        log.info("获取知识列表");
        // 设置当前用户ID
        knowledgeListDTO.setUserId(BaseContext.getCurrentId());
        PageResult pageResult = knowledgeService.pageQuery(knowledgeListDTO);
        return Result.success(pageResult);
    }

    /**
     * 创建知识库
     * @param knowledgeAddDTO
     * @return
     */
    @PostMapping("/create")
    public Result add(@RequestBody KnowledgeAddDTO knowledgeAddDTO) {
        log.info("创建知识: {}", knowledgeAddDTO.getKnowledgeName());
        knowledgeService.add(knowledgeAddDTO);
        return Result.success();
    }

    /**
     * 删除知识库
     * @param knowledgeCode
     * @return
     */
    @DeleteMapping("/delete/{knowledgeCode}")
    public Result<String> delete(@PathVariable("knowledgeCode") String knowledgeCode) {
        log.info("删除知识: {}", knowledgeCode);
        knowledgeService.deleteByKnowledgeCode(knowledgeCode);
        return Result.success("删除成功");
    }

    /**
     * 上传知识库文件
     * @param uploadFile 上传的文件
     * @return 文件访问URL
     */
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("uploadFile") MultipartFile uploadFile, @RequestParam("knowledgeCode") String knowledgeCode) {
        log.info("上传文件：{}", uploadFile.getOriginalFilename());
        String url = knowledgeService.upload(uploadFile, knowledgeCode);
        return Result.success(url);
    }

    /**
     * 获取知识库文件列表
     * @param knowledgeCode 知识库编码
     * @return 文件列表
     */
    @GetMapping("/files/{knowledgeCode}")
    public Result<List<KnowledgeFileVO>> fileList(@PathVariable("knowledgeCode") String knowledgeCode) {
        log.info("获取知识库文件列表，知识库编码：{}", knowledgeCode);
        List<KnowledgeFileVO> fileList = knowledgeService.getKnowledgeFilesByCode(knowledgeCode);
        return Result.success(fileList);
    }

    /**
     * 获取文件内容
     * @param fileName 文件名
     * @return 文件内容
     */
    @GetMapping("/file/content/{fileName}")
    public Result<String> getFileContent(@PathVariable("fileName") String fileName) {
        log.info("获取文件内容，文件名：{}", fileName);
        String fileContent = knowledgeService.getFileContent(fileName);
        return Result.success(fileContent);
    }

    /**
     * 删除文件
     * @param fileId 文件ID
     * @return 删除结果
     */
    @DeleteMapping("/file/{fileId}")
    public Result<String> deleteFile(@PathVariable("fileId") Integer fileId) {
        log.info("删除文件，文件ID：{}", fileId);
        knowledgeService.deleteFileById(fileId);
        return Result.success("删除成功");
    }

    
}
